/**
 * 
 */
package com.maoye.ossp.sys.tpl.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.MessageTemplate;
import com.maoye.ossp.sys.base.service.MessageTemplateService;

/**
 *  
 * @author xiaohan
 * @date 2014年3月24日
 * @version 
 * @filename com.maoye.ossp.sys.tpl.controller.MessageTemplateController.java 
 */

@Controller
@RequestMapping(value = "/sys/msg/tpl/")
public class MessageTemplateController extends BaseController {
	Logger logger = Logger.getLogger(MessageTemplateController.class);
	
	@Resource
	MessageTemplateService service;
	
	String listUrl = "redirect:/sys/msg/tpl/list/1/100";
	
	@RequestMapping("list/{pageIndex}/{pageSize}")
	public String list(MessageTemplate tpl,Model model,
			@PathVariable("pageIndex")int pageIndex,@PathVariable("pageSize")int pageSize) {
		try {
			List<MessageTemplate> list = service.getListByCode(tpl.getCode(), pageIndex, pageSize);
			model.addAttribute("list", list);
			model.addAttribute("obj", tpl);
		} catch (Exception e) {
			logger.debug(e);
			addMessage(model, e.getMessage());
		}
		return "/sys/msg/tpl/list";
	}
	
	@RequestMapping("{id}/view")
	public String updateView(@PathVariable("id")Long id,Model model,RedirectAttributes redirectAttributes) {
		boolean isSuccess = false;
		try {
			MessageTemplate tpl_temp = service.getById(id);
			if (isSuccess = (tpl_temp != null)) {
				model.addAttribute("modifyFlag", "update");
				model.addAttribute("obj", tpl_temp);
			}else {
				addMessage(redirectAttributes, "空数据!");
			}
		} catch (Exception e) {
			logger.debug(e);
			addMessage(model, e.getMessage());
		}
		return isSuccess ? "/sys/msg/tpl/edit" : listUrl;
	}
	@RequestMapping("/view")
	public String addView(MessageTemplate tpl,Model model,RedirectAttributes redirectAttributes) {
		try {
				model.addAttribute("obj", new MessageTemplate());
				model.addAttribute("modifyFlag", "add");
		} catch (Exception e) {
			logger.debug(e);
		}
		return "/sys/msg/tpl/edit" ;
	}
	/**
	 * 修改
	 * @param tpl
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author xiaohan
	 * @data 2014年3月24日
	 */
	@RequestMapping("/update")
	public String update(MessageTemplate tpl,Model model,RedirectAttributes redirectAttributes) {
		boolean isSuccess = false;
		try {
			isSuccess = (service.updateTemplate(tpl) > 0);
		} catch (Exception e) {
			logger.debug(e);
			addMessage(model, "修改失败,确认是否Code重复!");
		}
		if (!isSuccess) {
			model.addAttribute("modifyFlag", "update");
			model.addAttribute("obj", tpl);
			addMessage(model, "修改失败,确认是否Code重复!");
		}else {
			addMessage(redirectAttributes, "修改成功!" );
		}
		return isSuccess ? listUrl : "/sys/msg/tpl/edit";
	}
	/**
	 * 添加
	 * @param tpl
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @author xiaohan
	 * @data 2014年3月24日
	 */
	@RequestMapping("/add")
	public String add(MessageTemplate tpl,Model model,RedirectAttributes redirectAttributes) {
		boolean isSuccess = false;
		try {
			isSuccess = (service.addTemplate(tpl) > 0);
		} catch (Exception e) {
			logger.debug(e);
			addMessage(model, "添加失败,确认是否Code重复!");
		}
		return isSuccess ? listUrl : "/sys/msg/tpl/edit";
	}
	/**
	 * 删除
	 * @param idsStr
	 * @param redirectAttributes
	 * @return
	 * @author xiaohan
	 * @data 2014年3月24日
	 */
	@RequestMapping("/{idsStr}/delete")
	public String delete(@PathVariable("idsStr")String idsStr,RedirectAttributes redirectAttributes) {
		boolean isSuccess = false;
		try {
			if (!StringUtils.isBlank(idsStr)) {
				String[] l = idsStr.split(",");
				long[] ids = new long[l.length];
				for (int i = 0; i < ids.length; i++) {
					ids[i] = Long.parseLong(l[i]);
				}
				isSuccess = (service.deleteTemplate(ids) > 0);
			}
		} catch (Exception e) {
			logger.debug(e);
			addMessage(redirectAttributes, e.getMessage());
		}
		addMessage(redirectAttributes, isSuccess ? "删除成功!" : "删除失败!");
		return listUrl;
	}
	
	
}
